//package com.plian.system.service.zzd.impl;
//
//import com.baomidou.mybatisplus.core.toolkit.Wrappers;
//import com.plian.Tools.CollectionUtil;
//import com.plian.Tools.StringPool;
//import com.plian.Tools.StringUtil;
//import com.plian.system.dto.fr.indicators.IndicatorsValueDTO;
//import com.plian.system.dto.zzd.ZZDCompanyIndicatorsQuery;
//import com.plian.system.dto.zzd.ZZDIndicatorsQuery;
//import com.plian.system.dto.zzd.ZZDQuery;
//import com.plian.system.entity.hr.staff.Director;
//import com.plian.system.entity.hr.staff.StaffFiles;
//import com.plian.system.entity.hr.staff.Supervisor;
//import com.plian.system.entity.pm.property.ContributorBasics;
//import com.plian.system.entity.pm.property.PropertyBasics;
//import com.plian.system.entity.pm.realproperty.RealProperty;
//import com.plian.system.entity.sys.Org;
//import com.plian.system.enumeratea.ZZDIndicatorsEnum;
//import com.plian.system.enumeratea.ZZDSearchDataType;
//import com.plian.system.mapper.fr.indicators.IndicatorsValueMapper;
//import com.plian.system.properties.GzwProperties;
//import com.plian.system.service.fr.bulletin.INewBulletinService;
//import com.plian.system.service.fr.enterprise.IEnterpriseService;
//import com.plian.system.service.fr.indicatorsvalue.IIndicatorsReportService;
//import com.plian.system.service.hr.dept.IDeptService;
//import com.plian.system.service.hr.staff.IDirectorService;
//import com.plian.system.service.hr.staff.IStaffFilesService;
//import com.plian.system.service.hr.staff.ISupervisorService;
//import com.plian.system.service.pm.property.IContributorBasicsService;
//import com.plian.system.service.pm.property.IPropertyBasicsService;
//import com.plian.system.service.pm.realproperty.IRealPropertyService;
//import com.plian.system.service.sys.orgzation.IOrgService;
//import com.plian.system.service.zzd.IZZDFinanceService;
//import com.plian.system.service.zzd.IZZDSystemService;
//import com.plian.system.service.zzd.helper.ZZDHelper;
//import com.plian.system.vo.fr.indicatorsValue.IndicatorsValueVO;
//import com.plian.system.vo.zzd.finance.FinancialDataVO;
//import com.plian.system.vo.zzd.finance.IndicatorsUnitDetailVO;
//import com.plian.system.vo.zzd.sys.*;
//import com.plian.system.wrapper.hr.dept.DeptWrapper;
//import com.plian.system.wrapper.hr.staff.DirectorWrapper;
//import com.plian.system.wrapper.hr.staff.StaffFilesWrapper;
//import com.plian.system.wrapper.hr.staff.SupervisorWrapper;
//import com.plian.system.wrapper.pm.property.ContributorBasicsWrapper;
//import com.plian.system.wrapper.pm.property.PropertyBasicsWrapper;
//import com.plian.system.wrapper.pm.realproperty.RealPropertyWrapper;
//import lombok.AllArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.stereotype.Service;
//
//import java.math.BigDecimal;
//import java.util.*;
//import java.util.function.Function;
//import java.util.stream.Collectors;
//import java.util.stream.Stream;
//
//import static com.plian.system.constant.fr.IndicatorsCodeConstant.*;
//import static com.plian.system.constant.pm.RealPropertyConstant.REAL_PROPERTY_LAND_TYPE;
//
//@Slf4j
//@Service
//@AllArgsConstructor
//public class ZZDSystemServiceImpl implements IZZDSystemService {
//
//    private IOrgService orgService;
//    private IPropertyBasicsService propertyBasicsService;
//    private IStaffFilesService staffFilesService;
//    private IDeptService deptService;
//    private INewBulletinService newBulletinService;
//    private IEnterpriseService enterpriseService;
//    private IContributorBasicsService contributorBasicsService;
//    private IDirectorService directorService;
//    private ISupervisorService supervisorService;
//    private IRealPropertyService realPropertyService;
//
//    private IZZDFinanceService zzdFinanceService;
//    private IndicatorsValueMapper indicatorsValueMapper;
//    private RealPropertyWrapper realPropertyWrapper;
//    private StaffFilesWrapper staffFilesWrapper;
//    private DeptWrapper deptWrapper;
//
//    private PropertyBasicsWrapper propertyBasicsWrapper;
//
//    private ContributorBasicsWrapper contributorBasicsWrapper;
//
//    private IIndicatorsReportService indicatorsReportService;
//
//    private DirectorWrapper directorWrapper;
//
//    private SupervisorWrapper supervisorWrapper;
//
//    private GzwProperties gzwProperties;
//    //领导人员类型
//    private final static String LEADER_STAFF_TYPE = "027001";
//    //中层人员类型
//    private final static String MIDDLE_STAFF_TYPE = "027001";
//    //党员人员类型
//    private final static List<String> PARTY_MEMBER_TYPE_LIST = Stream.of("022001","022002").collect(Collectors.toList());
//
//    @Override
//    public List<QueryDataVO> search(ZZDQuery zzdQuery) {
//        Org jgOrg = orgService.getById(gzwProperties.getReportId());
//        List<QueryDataVO> queryDataVOS = Stream.of(
//                        staffFilesService.list(Wrappers.<StaffFiles>lambdaQuery()
//                                .like(StaffFiles::getStaffName, zzdQuery.getKeyword())),
//                        orgService.list(Wrappers.<Org>lambdaQuery()
//                                .likeRight(Org::getOrgCode, jgOrg.getOrgCode())
//                                .like(Org::getName, zzdQuery.getKeyword())),
//                        propertyBasicsService.list(Wrappers.<PropertyBasics>lambdaQuery()
//                                        .and(wrapper -> wrapper.isNull(PropertyBasics::getOrgId)
//                                                .or().ne(PropertyBasics::getCompetentOrganization, gzwProperties.getId()))
//                                .like(PropertyBasics::getOrgName, zzdQuery.getKeyword())),
//                        realPropertyService.list(Wrappers.<RealProperty>lambdaQuery()
//                                        .eq(RealProperty::getIsLatest, 2)
//                                        .and(wrapper -> wrapper.like(RealProperty::getHouseName, zzdQuery.getKeyword())
//                                                .or().like(RealProperty::getLandName, zzdQuery.getKeyword())))
//                                        .stream()
//                                        .collect(Collectors.toList()))
//                .flatMap(Collection::stream)
//               .map(obj -> {
//                    QueryDataVO queryDataVO = new QueryDataVO();
//                    if (obj instanceof StaffFiles) {
//                        StaffFiles staffFiles = (StaffFiles) obj;
//                        queryDataVO.setDataType(ZZDSearchDataType.STAFF_TYPE.getDataType());
//                        queryDataVO.setName(staffFiles.getStaffName());
//                        queryDataVO.setSearchId(String.valueOf(staffFiles.getId()));
//                    } else if (obj instanceof Org) {
//                        Org org = (Org) obj;
//                        queryDataVO.setDataType(ZZDSearchDataType.ORG_TYPE.getDataType());
//                        queryDataVO.setName(org.getName());
//                        queryDataVO.setSearchId(org.getId());
//                    } else if (obj instanceof PropertyBasics) {
//                        PropertyBasics propertyBasics = (PropertyBasics) obj;
//                        queryDataVO.setDataType(ZZDSearchDataType.PROPERTY_TYPE.getDataType());
//                        queryDataVO.setName(propertyBasics.getOrgName());
//                        queryDataVO.setSearchId(propertyBasics.getId());
//                    }else if (obj instanceof RealProperty) {
//                        RealProperty realProperty = (RealProperty) obj;
//                        queryDataVO.setDataType(ZZDSearchDataType.REAL_PROPERTY_TYPE.getDataType());
//                        queryDataVO.setName(realProperty.getRealPropertyNature() == REAL_PROPERTY_LAND_TYPE
//                                ? realProperty.getLandName() : realProperty.getHouseName());
//                        queryDataVO.setSearchId(String.valueOf(realProperty.getId()));
//                    }
//                    return queryDataVO;
//                })
//                .collect(Collectors.toList());
//        return queryDataVOS;
//    }
//
//    @Override
//    public QueryDetailVO searchDetail(QueryDataVO queryDataVO) {
//        QueryDetailVO queryDetailVO = new QueryDetailVO();
//        switch (ZZDSearchDataType.getByDataType(queryDataVO.getDataType())){
//            case STAFF_TYPE:
//                StaffFiles staffFiles = staffFilesService.getById(queryDataVO.getSearchId());
//                if (staffFiles == null){
//                    return queryDetailVO;
//                }
//                queryDetailVO.setStaffFilesVO(staffFilesWrapper.entityToVO(staffFiles));
//                if (StringUtil.isNotBlank(staffFiles.getDepartmentNameId())){
//                    queryDetailVO.setDeptVO(deptWrapper.entityToVO(deptService.getDetailVO(staffFiles.getDepartmentNameId())));
//                }
//                return queryDetailVO;
//            case ORG_TYPE:
//                queryDetailVO.setEnterpriseInfo(enterpriseService.findByOrgId(queryDataVO.getSearchId()));
//                String reportTime = newBulletinService.getMaxReportTimeByOrgId(queryDataVO.getSearchId());
//                queryDetailVO.setFinancialDataVO(getFinancialByOrgId(queryDataVO.getSearchId(), reportTime));
//                queryDetailVO.setPropertyBasicsVO(propertyBasicsService.findVOByOrgId(queryDataVO.getSearchId()));
//                queryDetailVO.setIndicatorTrendsVO(zzdFinanceService.getIndicatorTrends(ZZDCompanyIndicatorsQuery.builder().queryType(1)
//                        .columnName(ZZDIndicatorsEnum.TOTAL_ASSETS.getColumnName()).orgId(queryDataVO.getSearchId()).build()));
//                if (queryDetailVO.getPropertyBasicsVO() != null){
//                    queryDetailVO.setContributorBasicsList(contributorBasicsWrapper.entityToVO(contributorBasicsService.list(Wrappers.<ContributorBasics>lambdaQuery()
//                            .eq(ContributorBasics::getPropertyId, queryDetailVO.getPropertyBasicsVO().getId()))));
//                }
//                //企业人员信息
//                StaffDataVO staffDataVO = new StaffDataVO();
//                List<StaffFiles> staffFilesList = staffFilesService.list(Wrappers.<StaffFiles>lambdaQuery()
//                        .eq(StaffFiles::getDef18, 1).eq(StaffFiles::getIsOut, 1)
//                        .eq(StaffFiles::getCompanyNameId, queryDataVO.getSearchId()));
//                if (CollectionUtil.isNotEmpty(staffFilesList)){
//                    staffDataVO.setTotalSum(staffFilesList.size());
//                    List<PersonnelSimpleInfoVO> leaderList = staffFilesWrapper.entityToPersonnelSimpleInfoVO(staffFilesList.stream()
//                            .filter(staff -> LEADER_STAFF_TYPE.equals(staff.getManagementCategoryId()))
//                            .collect(Collectors.toList()));
//                    staffDataVO.setLeaderSum(0);
//                    if (CollectionUtil.isNotEmpty(leaderList)){
//                        staffDataVO.setLeaderList(leaderList);
//                        staffDataVO.setLeaderSum(leaderList.size());
//                    }
//                    staffDataVO.setPartyMemberSum((int) staffFilesList.stream().filter(staff -> PARTY_MEMBER_TYPE_LIST.contains(staff.getPoliticCountenanceId())).count());
//                }
//                List<PersonnelSimpleInfoVO> directorList = directorWrapper.entityToPersonnelSimpleInfoVO(directorService.list(Wrappers.<Director>lambdaQuery()
//                        .eq(Director::getCompanyId, queryDataVO.getSearchId())));
//                staffDataVO.setDirectorSum(0);
//                if (CollectionUtil.isNotEmpty(directorList)){
//                    staffDataVO.setDirectorList(directorList);
//                    staffDataVO.setDirectorSum(directorList.size());
//                }
//                List<PersonnelSimpleInfoVO> supervisorList = supervisorWrapper.entityToPersonnelSimpleInfoVO(supervisorService.list(Wrappers.<Supervisor>lambdaQuery()
//                        .eq(Supervisor::getCompanyId, queryDataVO.getSearchId())));
//                staffDataVO.setDirectorSum(0);
//                if (CollectionUtil.isNotEmpty(supervisorList)){
//                    staffDataVO.setSupervisorList(supervisorList);
//                    staffDataVO.setSupervisorSum(supervisorList.size());
//                }
//                queryDetailVO.setStaffDataVO(staffDataVO);
//                return queryDetailVO;
//            case PROPERTY_TYPE:
//                queryDetailVO.setPropertyBasicsVO(propertyBasicsWrapper.entityToVO(propertyBasicsService.getById(queryDataVO.getSearchId())));
//                if (queryDetailVO.getPropertyBasicsVO() != null){
//                    queryDetailVO.setContributorBasicsList(contributorBasicsWrapper.entityToVO(contributorBasicsService.list(Wrappers.<ContributorBasics>lambdaQuery()
//                            .eq(ContributorBasics::getPropertyId, queryDetailVO.getPropertyBasicsVO().getId()))));
//                }
//                return queryDetailVO;
//            case REAL_PROPERTY_TYPE:
//                queryDetailVO.setRealPropertyVO(realPropertyWrapper.entityToVO(realPropertyService.getById(queryDataVO.getSearchId())));
//                return queryDetailVO;
//
//        }
//        return null;
//    }
//
//    @Override
//    public OverviewIndicatorsValueVO indicatorsDetail(ZZDIndicatorsQuery zzdIndicatorsQuery) {
//        ZZDIndicatorsEnum zzdIndicatorsEnum = ZZDIndicatorsEnum.getByColumnName(zzdIndicatorsQuery.getColumnName());
//        zzdIndicatorsQuery.setColumnCode(zzdIndicatorsEnum.getColumnCode());
//        Function<ZZDIndicatorsQuery, OverviewIndicatorsValueVO> function = zzdIndicatorsEnum.getFunction();
//        OverviewIndicatorsValueVO overviewIndicatorsValueVO = function.apply(zzdIndicatorsQuery);
//        if (overviewIndicatorsValueVO != null
//                && CollectionUtil.isNotEmpty(overviewIndicatorsValueVO.getUnitDetailList())){
//            switch (zzdIndicatorsQuery.getOrderType()){
//                case 1:
//                    overviewIndicatorsValueVO.setUnitDetailList(overviewIndicatorsValueVO.getUnitDetailList().stream()
//                            .sorted(Comparator.comparing(IndicatorsUnitDetailVO::getValue, Comparator.nullsFirst(Comparator.naturalOrder())).reversed())
//                            .collect(Collectors.toList()));
//                    break;
//                case 2:
//                    overviewIndicatorsValueVO.setUnitDetailList(overviewIndicatorsValueVO.getUnitDetailList().stream()
//                            .sorted(Comparator.comparing(IndicatorsUnitDetailVO::getOrderNum, Comparator.nullsFirst(Comparator.naturalOrder())))
//                            .collect(Collectors.toList()));
//                    break;
//                case 3:
//                    overviewIndicatorsValueVO.setUnitDetailList(overviewIndicatorsValueVO.getUnitDetailList().stream()
//                            .sorted(Comparator.comparing(IndicatorsUnitDetailVO::getValue, Comparator.nullsFirst(Comparator.naturalOrder())))
//                            .collect(Collectors.toList()));
//                    break;
//            }
//        }
//        return overviewIndicatorsValueVO;
//    }
//
//    /**
//     * 快报指标值获取
//     * @param orgId
//     * @param reportTime
//     * @return
//     */
//    private FinancialDataVO getFinancialByOrgId(String orgId, String reportTime){
//        String[] reportTimes = reportTime.split(StringPool.DASH);
//        IndicatorsValueDTO indicatorsValueDTO = new IndicatorsValueDTO();
//        indicatorsValueDTO.setYear(reportTimes[0]);
//        indicatorsValueDTO.setMonth(reportTimes[1]);
//        indicatorsValueDTO.setOrgId(orgId);
//        indicatorsValueDTO.setCodes(Stream.of(K22, K30, K36, K33, KLJ1, KLJ19, K58, KSUM40).collect(Collectors.toList()));
//        List<IndicatorsValueVO> indicatorsValueVOS = new ArrayList<>();
//        boolean flag = orgService.haveChild(orgId);
//        if (flag){
//            indicatorsValueVOS = indicatorsValueMapper.queryGroupValueByCode(indicatorsValueDTO);
//        }else {
//            indicatorsValueVOS = indicatorsValueMapper.querySingleValueByCode(indicatorsValueDTO);
//        }
//        Map<String, BigDecimal> codeValueMap = ZZDHelper
//                .indicatorsValueToValueMap(Integer.valueOf(reportTimes[0]), indicatorsValueVOS, null, null,
//                        Stream.of(orgId).collect(Collectors.toList()));
//        FinancialDataVO financialDataVO = ZZDHelper.codeValueMapToDataVO(codeValueMap, new BigDecimal(10000));
//        if (flag){
//            financialDataVO.setNetAssetsRoe(indicatorsReportService
//                    .getNetAssets(Stream.of(orgId).collect(Collectors.toList()), null, reportTime));
//        }else {
//            financialDataVO.setNetAssetsRoe(indicatorsReportService
//                    .getNetAssets(null, Stream.of(orgId).collect(Collectors.toList()), reportTime));
//        }
//        return financialDataVO;
//    }
//
//
//}
